package com.kh.pajx.sop.util.web;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.kh.pajx.sop.util.security.encoder.DESEncoderUtil;

public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {

		try {
			// 数据库用户名
			String username = props.getProperty(Constants.JDBC_DATASOURCE_USERNAME_KEY);
			if (username != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_USERNAME_KEY, DESEncoderUtil.decrypt(username));
			}
			String username2 = props.getProperty(Constants.JDBC_DATASOURCE_USERNAME_KEY2);
			if (username2 != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_USERNAME_KEY2, DESEncoderUtil.decrypt(username2));
			}
			
			// 数据库密码
			String password = props.getProperty(Constants.JDBC_DATASOURCE_PASSWORD_KEY);
			if (password != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_PASSWORD_KEY, DESEncoderUtil.decrypt(password));
			}
			
			String password2 = props.getProperty(Constants.JDBC_DATASOURCE_PASSWORD_KEY2);
			if (password2 != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_PASSWORD_KEY2, DESEncoderUtil.decrypt(password2));
			}


			// 连接字符串
			String url = props.getProperty(Constants.JDBC_DATASOURCE_URL_KEY);
			if (url != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_URL_KEY, DESEncoderUtil.decrypt(url));
			}
			
			String url2 = props.getProperty(Constants.JDBC_DATASOURCE_URL_KEY2);
			if (url2 != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_URL_KEY2, DESEncoderUtil.decrypt(url2));
			}

			// 数据库驱动
			String driverClassName = props.getProperty(Constants.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY);
			if (driverClassName != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY,
						DESEncoderUtil.decrypt(driverClassName));
			}
			
			String driverClassName2 = props.getProperty(Constants.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY2);
			if (driverClassName2 != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY2,
						DESEncoderUtil.decrypt(driverClassName2));
			}
			
			
			// 数据库类型
			String dbtype = props.getProperty(Constants.JDBC_DATASOURCE_TYPE_KEY);
			if (dbtype != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_TYPE_KEY, DESEncoderUtil.decrypt(dbtype));
			}
			
			String dbtype2 = props.getProperty(Constants.JDBC_DATASOURCE_TYPE_KEY2);
			if (dbtype2 != null) {
				props.setProperty(Constants.JDBC_DATASOURCE_TYPE_KEY2, DESEncoderUtil.decrypt(dbtype2));
			}
			
			
			super.processProperties(beanFactoryToProcess, props);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BeanInitializationException(e.getMessage());
		}

	}

}
